<template>
    <PageHeader>
        <view class="PAGE_TITLE">搜索</view>
    </PageHeader>
    <view>
        <uni-search-bar radius="5" placeholder="请搜索你想要的内容" cancelButton="always" @input="search" @cancel="cancel" />
    </view>
    <view class="p-l-r" :class="$style.body">
        <template v-if="keyname.length == 0">
            <view :class="$style.history" v-if="historyList.length > 0">
                <view :class="$style.top">
                    <view :class="$style.name">历史记录</view>
                    <template v-if="delStatus">
                        <view @click="delAll">全部删除</view>
                        <view class="m-l-20" @click="toggleDelStatus">完成</view>
                    </template>
                    <view v-else>
                        <uni-icons type="trash" size="20" @click="toggleDelStatus"></uni-icons>
                    </view>
                    
                </view>
                <view :class="$style.items">
                    <AppLink v-for="(item, index) in historyList" :key="index" :disabled="delStatus" :url="item.url" :id="item.id" :name="item.name" :link-type="item.linkType" :qr="item.qr" :class="[$style.item, delStatus ? $style.del : '']">
                        <text>{{ item.name }}</text>
                        <uni-icons :class="$style.del" @click="delOne(index)" type="closeempty" size="10"></uni-icons>
                    </AppLink>
                </view>
            </view>
        </template>
        <template v-else>
            <uni-load-more v-if="searchLoading" status="loading" :showText="false"></uni-load-more>
            <template v-else>
                <template v-if="searchRes.length == 0">
                    <view v-if="searched">无相关结果</view>
                </template>
                <view v-else :class="$style.searchItems">
                    <AppLink :class="$style.item" v-for="item in searchRes" :url="item.url" :id="item.id" :name="item.name" :link-type="item.linkType" :qr="item.qr" @success="clickApp">
                        <uni-icons type="search" size="16"></uni-icons>
                        <view class="m-l-20" :class="$style.text">
                            <template v-if="item.before">{{ item.before }}</template>
                            <text :class="$style.keyname">{{ item.key }}</text>
                            <template v-if="item.after">{{ item.after }}</template>
                        </view>
                    </AppLink>
                </view>
            </template>
        </template>

        
    </view>


    <AppQr />
</template>

<script lang="ts" setup>
import PageHeader from '@/components/PageHeader.vue'
    import { searchApi } from '@/api/Apps';
import { ref } from 'vue';
import AppLink from '@/components/AppLink.vue'
import AppQr from '@/components/AppQr.vue'

    const cacheKey = 'search_history'

    const historyList = ref<any[]>([])
    const searchRes = ref<any[]>([])

    const cache = localStorage.getItem(cacheKey)
    if(cache) {
        historyList.value = JSON.parse(cache)
    }


    const keyname = ref<string>('')
    const searchLoading = ref(false)
    const searched = ref(false)

    function clickApp(data) {
        const i = historyList.value.findIndex(v => v.id == data.id)
        if(i >= 0) {
            historyList.value.splice(i, 1)
        }
        historyList.value.unshift(data)
        localStorage.setItem(cacheKey, JSON.stringify(historyList.value))

    }

    const delStatus = ref(false)

    function toggleDelStatus() {
        delStatus.value = !delStatus.value
    }

    function delAll() {
        historyList.value.splice(0)
        localStorage.removeItem(cacheKey)
    }

    function delOne(i:number) {
        historyList.value.splice(i, 1)
        localStorage.setItem(cacheKey, JSON.stringify(historyList.value))
    }

    let  t:any = null

    function search(key) {
        searched.value = false
        if(t) clearTimeout(t)
        keyname.value = key
        if(key) {
            t = setTimeout(() => {
                searchLoading.value = true
                searchRes.value.splice(0)
                
                searchApi(key).then(r => {
                    searchLoading.value = false
                    searched.value = true
                    if(r.data) {
                        r.data.forEach(item => {console.log(key)
                            const originalText = item.name as string
                            var index = originalText.indexOf(key);
                            var beforeText = originalText.substring(0, index);

                            var highlightedText = originalText.substring(index, index + key.length);

                            console.log(highlightedText)
                            var afterText = originalText.substring(index + key.length);
                            searchRes.value.push({
                                before : beforeText,
                                after : afterText,
                                key : highlightedText,
                                url : item.url,
                                id : item.id,
                                name : originalText,
                                linkType : item.linkType,
                                qr:item.qr
                            })
                            
                        })
                    }
                })
            }, 300)
        }
    }
    function cancel() {
        uni.navigateBack()
    }
</script>

<style lang="scss" module>
    .body{
        margin-top: 40rpx;
    }

    .searchItems{
        .item{
            display:flex;
            align-items: center;
            padding: 30rpx 0;
            border-bottom: 1px solid #E7E7E7;
            
        }

        .text{
            font-size: 28rpx;
            .keyname{
                color: $primary-primary;
            }
        }
    }

    

    .history{
        .top{
            display: flex;
            align-items: center;
        }

        .name{
            font-size: 36rpx;
            color: #000;
            flex: 1;
        }

        .items{
            margin-top: 30rpx;
            display: flex;
            gap: 10rpx;
            flex-wrap: wrap;
            .item{
                background-color: #F3F3F3;
                font-size: 24rpx;
                color: #000;
                padding: 4rpx 16rpx;
                border-radius: 6rpx;

                .del{
                    display: none;
                }
                
                &.del{
                    display: inline-flex;
                    align-items: center;
                    

                    .del{
                        display: block;
                        margin-left: 6rpx;
                    }
                }
            }
        }
    }
</style>